package com.eastfair.home.service.impl;

import com.eastfair.builder.enumeration.ReportSpaceHomeAuditStatusEnum;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.entity.Drawing;
import com.eastfair.home.entity.ReportDrawingAudit;
import com.eastfair.home.entity.ReportDrawingCategoryAudit;
import com.eastfair.home.entity.ReportSpaceAudit;
import com.eastfair.home.enumeration.ReportSpaceAuditDrawingAuditStatusEnum;
import com.eastfair.home.service.*;
import com.eastfair.venuebooking.api.HomeBuilderDrawingCategoryAuditFeign;
import com.eastfair.venueservice.dto.HomeBuilderDrawingAuditDTO;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditDTO;
import com.eastfair.venueservice.enumeration.HomeBuilderDrawingCategoryAuditAuditStatusEnum;
import com.eastfair.venueservice.enumeration.HomeBuilderDrawingCategoryAuditDrawingSourceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 主场搭建商图纸分类审核 service.
 *
 * @author dqq
 * @date 2023 /4/28
 */
@Service
@Slf4j
public class HomeHomeBuilderDrawingCategoryAuditServiceImpl implements HomeHomeBuilderDrawingCategoryAuditService {

    @Resource
    private HomeBuilderDrawingCategoryAuditFeign homeBuilderDrawingCategoryAuditFeign;

    @Resource
    private ReportSpaceAuditService reportSpaceAuditService;

    @Resource
    private ReportDrawingCategoryAuditService reportDrawingCategoryAuditService;

    @Resource
    private ReportDrawingAuditService reportDrawingAuditService;

    @Resource
    private DrawingService drawingService;

    @Override
    public boolean saveBatch(List<HomeBuilderDrawingCategoryAuditDTO> list) {
        log.info("saveBatch - 批量保存主场搭建商图纸分类审核记录, list={}", list);
        R<Boolean> result = homeBuilderDrawingCategoryAuditFeign.saveBatch(list);
        log.info("saveBatch - 批量保存主场搭建商图纸分类审核记录, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByReportDrawingCategoryAuditList(List<ReportDrawingCategoryAudit> reportDrawingCategoryAuditList, List<ReportDrawingAudit> reportDrawingAuditList, ReportSpaceAudit reportSpaceAudit) {
        // 图纸审核记录的map
        Map<Long, List<ReportDrawingAudit>> reportDrawingAuditMap = reportDrawingAuditList
                .stream()
                .collect(Collectors.groupingBy(ReportDrawingAudit::getReportDrawingCategoryAuditId));
        // 查询主场设置的图纸分类
        List<Long> drawingCategoryIdList = reportDrawingCategoryAuditList
                .stream()
                .map(ReportDrawingCategoryAudit::getDrawingCategoryId)
                .collect(Collectors.toList());
        List<Drawing> drawingList = drawingService.listByIds(drawingCategoryIdList);
        Map<Long, Drawing> drawingMap = new HashMap<>(16);
        drawingList.forEach(drawing -> drawingMap.put(drawing.getId(), drawing));
        // 主场审核通过之后，再推给场馆审核
        List<HomeBuilderDrawingCategoryAuditDTO> homeBuilderDrawingCategoryAuditList = new ArrayList<>();
        reportDrawingCategoryAuditList.forEach(reportDrawingCategoryAudit -> {
            HomeBuilderDrawingCategoryAuditDTO homeBuilderDrawingCategoryAuditDTO = toHomeBuilderDrawingCategoryAuditDTO(reportDrawingCategoryAudit, reportSpaceAudit, reportDrawingAuditMap, drawingMap);
            homeBuilderDrawingCategoryAuditList.add(homeBuilderDrawingCategoryAuditDTO);
        });
        saveBatch(homeBuilderDrawingCategoryAuditList);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByReportDrawingCategoryAuditList(List<ReportDrawingCategoryAudit> reportDrawingCategoryAuditList, List<ReportDrawingAudit> reportDrawingAuditList) {
        ReportDrawingCategoryAudit reportDrawingCategoryAudit = reportDrawingCategoryAuditList.get(0);
        ReportSpaceAudit reportSpaceAudit = reportSpaceAuditService.getByReportSpaceId(reportDrawingCategoryAudit.getReportSpaceId());
        // 只有主场图纸审核通过，才向场馆提交图纸
        if (!ReportSpaceAuditDrawingAuditStatusEnum.APPROVED.eq(reportSpaceAudit.getDrawingAuditStatus())) {
            return false;
        }
        saveBatchByReportDrawingCategoryAuditList(reportDrawingCategoryAuditList, reportDrawingAuditList, reportSpaceAudit);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByReportSpaceAudit(ReportSpaceAudit reportSpaceAudit) {
        if (!ReportSpaceHomeAuditStatusEnum.APPROVED.eq(reportSpaceAudit.getHomeAuditStatus())) {
            return false;
        }
        Long reportSpaceId = reportSpaceAudit.getReportSpaceId();
        List<ReportDrawingCategoryAudit> reportDrawingCategoryAuditList = reportDrawingCategoryAuditService.listByReportSpaceId(reportSpaceId);
        List<ReportDrawingAudit> reportDrawingAuditList = reportDrawingAuditService.listByReportSpaceId(reportSpaceId);
        return saveBatchByReportDrawingCategoryAuditList(reportDrawingCategoryAuditList, reportDrawingAuditList, reportSpaceAudit);
    }

    @Override
    public boolean removeByReportId(Long reportId, HomeBuilderDrawingCategoryAuditDrawingSourceEnum drawingSource) {
        log.info("removeByReportId - 报馆ID删除图纸分类审核, reportId={}, drawingSource={}", reportId, drawingSource);
        R<Boolean> result = homeBuilderDrawingCategoryAuditFeign.removeByReportId(reportId, drawingSource);
        log.info("removeByReportId - 报馆ID删除图纸分类审核, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    /**
     * ReportDrawingCategoryAudit 主场图纸分类审核 转为HomeBuilderDrawingCategoryAuditDTO 场馆图纸分类审核
     *
     * @param reportDrawingCategoryAudit the report drawing category audit
     * @param reportSpaceAudit           the report space audit
     * @param reportDrawingAuditMap      the report drawing audit map
     * @param drawingMap                 the drawing map
     * @return the home builder drawing category audit dto
     */
    private HomeBuilderDrawingCategoryAuditDTO toHomeBuilderDrawingCategoryAuditDTO(ReportDrawingCategoryAudit reportDrawingCategoryAudit,
                                                                                   ReportSpaceAudit reportSpaceAudit,
                                                                                   Map<Long, List<ReportDrawingAudit>> reportDrawingAuditMap,
                                                                                   Map<Long, Drawing> drawingMap) {
        HomeBuilderDrawingCategoryAuditDTO homeBuilderDrawingCategoryAuditDTO = new HomeBuilderDrawingCategoryAuditDTO();
        homeBuilderDrawingCategoryAuditDTO.setReportId(reportDrawingCategoryAudit.getReportSpaceId());
        homeBuilderDrawingCategoryAuditDTO.setDrawingSource(HomeBuilderDrawingCategoryAuditDrawingSourceEnum.BUILDER_REPORT);
        homeBuilderDrawingCategoryAuditDTO.setExhibitionManageId(reportSpaceAudit.getExhibitionId());
        homeBuilderDrawingCategoryAuditDTO.setExhibitionManageName(reportSpaceAudit.getExhibitionName());
        homeBuilderDrawingCategoryAuditDTO.setSpaceCode(reportSpaceAudit.getSpaceCode());
        homeBuilderDrawingCategoryAuditDTO.setSpaceName(reportSpaceAudit.getSpaceName());
        homeBuilderDrawingCategoryAuditDTO.setSiteCode(reportSpaceAudit.getSiteCode());
        homeBuilderDrawingCategoryAuditDTO.setSiteName(reportSpaceAudit.getSiteName());
        homeBuilderDrawingCategoryAuditDTO.setDrawingId(reportDrawingCategoryAudit.getDrawingCategoryId());
        homeBuilderDrawingCategoryAuditDTO.setDrawingName(reportDrawingCategoryAudit.getDrawingCategoryName());
        homeBuilderDrawingCategoryAuditDTO.setDrawingSort(reportDrawingCategoryAudit.getDrawingCategorySort());
        Drawing drawing = drawingMap.get(reportDrawingCategoryAudit.getDrawingCategoryId());
        if (drawing != null) {
            homeBuilderDrawingCategoryAuditDTO.setDrawingType(drawing.getDrawingType());
        }
        homeBuilderDrawingCategoryAuditDTO.setReportTime(LocalDateTime.now());
        homeBuilderDrawingCategoryAuditDTO.setHomeAuditStatus(reportDrawingCategoryAudit.getAuditStatus().getCode());
        homeBuilderDrawingCategoryAuditDTO.setHomeAuditOpinion(reportDrawingCategoryAudit.getAuditOpinion());
        HomeBuilderDrawingCategoryAuditAuditStatusEnum venueAuditStatus = HomeBuilderDrawingCategoryAuditAuditStatusEnum.get(reportDrawingCategoryAudit.getVenueAuditStatus().getCode());
        homeBuilderDrawingCategoryAuditDTO.setAuditStatus(venueAuditStatus);
        List<HomeBuilderDrawingAuditDTO> homeBuilderDrawingAuditList = new ArrayList<>();
        List<ReportDrawingAudit> reportDrawingAuditList = reportDrawingAuditMap.get(reportDrawingCategoryAudit.getId());
        if (reportDrawingAuditList != null && !reportDrawingAuditList.isEmpty()) {
            for (ReportDrawingAudit reportDrawingAudit : reportDrawingAuditList) {
                HomeBuilderDrawingAuditDTO homeBuilderDrawingAuditDTO = toHomeBuilderDrawingAuditDTO(reportDrawingAudit, reportSpaceAudit);
                homeBuilderDrawingAuditList.add(homeBuilderDrawingAuditDTO);
            }
        }
        homeBuilderDrawingCategoryAuditDTO.setDrawingAuditList(homeBuilderDrawingAuditList);
        return homeBuilderDrawingCategoryAuditDTO;
    }

    /**
     * ReportDrawingAudit 主场图纸审核 转为 HomeBuilderDrawingAuditDTO 场馆图纸审核
     *
     * @param reportDrawingAudit the report drawing audit
     * @param reportSpaceAudit   the report space audit
     * @return the home builder drawing audit dto
     */
    private HomeBuilderDrawingAuditDTO toHomeBuilderDrawingAuditDTO(ReportDrawingAudit reportDrawingAudit, ReportSpaceAudit reportSpaceAudit) {
        HomeBuilderDrawingAuditDTO homeBuilderDrawingAuditDTO = new HomeBuilderDrawingAuditDTO();
        homeBuilderDrawingAuditDTO.setReportId(reportDrawingAudit.getReportSpaceId());
        homeBuilderDrawingAuditDTO.setDrawingSource(HomeBuilderDrawingCategoryAuditDrawingSourceEnum.BUILDER_REPORT.getCode());
        homeBuilderDrawingAuditDTO.setSpaceCode(reportSpaceAudit.getSpaceCode());
        homeBuilderDrawingAuditDTO.setSpaceName(reportSpaceAudit.getSpaceName());
        homeBuilderDrawingAuditDTO.setSiteCode(reportSpaceAudit.getSiteCode());
        homeBuilderDrawingAuditDTO.setSiteName(reportSpaceAudit.getSiteName());
        homeBuilderDrawingAuditDTO.setExhibitionManageId(reportSpaceAudit.getExhibitionId());
        homeBuilderDrawingAuditDTO.setExhibitionManageName(reportSpaceAudit.getExhibitionName());
        homeBuilderDrawingAuditDTO.setDrawingId(reportDrawingAudit.getDrawingCategoryId());
        homeBuilderDrawingAuditDTO.setDrawingName(reportDrawingAudit.getDrawingCategoryName());
        homeBuilderDrawingAuditDTO.setImageUrl(reportDrawingAudit.getImageUrl());
        return homeBuilderDrawingAuditDTO;
    }
}